# Copyright (C) 2009 Martin Kirsche

import time
from optparse import OptionParser

from LastFmPlayer import LastFmPlayer


def main():    
    player = LastFmPlayer()
    
    parser = OptionParser()
    parser.add_option("-u", "--user", type="string", dest="user")
    parser.add_option("-p", "--password", type="string", dest="password")
    parser.add_option("-b", "--buffer-size", type="int", dest="bufferSize", default=player.bufferSize)
    parser.add_option("-a", "--adjust-station", type="string", dest="stationUrl", default=player.stationUrl)
    parser.add_option("-d", "--download-filename", type="string", dest="downloadFileName", default=player.downloadFileName)
    parser.add_option("-t", "--download-temp-filename", type="string", dest="downloadTempFileName", default=player.downloadTempFileName)
    parser.add_option("-o", "--output-to-stdout", action="store_true", dest="outputToStdOut", default=False)
    parser.add_option("-i", "--create-id3", action="store_true", dest="createId3", default=False)
    parser.add_option("-e", "--ignore-errors", action="store_true", dest="ignoreErrors", default=False)
    parser.add_option("-g", "--show-gui", action="store_true", dest="showGui", default=False)
    parser.add_option("-r", "--remote-controls", type="string", action="append", dest="remoteControls", default=[])
    (options, args) = parser.parse_args()
    
    copy(options, player)
    
    if player.remoteControls:
        import RemoteControl
        remoteControl = RemoteControl.RemoteControl(player.remoteControls)
        remoteControl.player = player
        remoteControl.MainLoop()
        return
    if player.showGui:
        import PlayerGui
        app = PlayerGui.PlayerApp(0)
        app.player = player
        app.MainLoop()
        return
    
    player.play()
    while True: time.sleep(player.poll()/1000.0)


def copy(source, target):
    for key in source.__dict__:
        if key.startswith('_'):
            continue
        value = getattr(source, key)
        if type(value) == 'function':
            continue
        setattr(target, key, value)
    return target


if __name__ == '__main__':
    main()
